#ifndef CDN_LIGHT_H
#define CDN_LIGHT_H
/*
*******************************************************************************
** Copyright (C), 2014-2020, TONLY AV R&D CENTER SOFTWARE DEVELOPMENT DEPARTMENT
** FileName: 
** <author>:             
** Date:
** Description:
**
** Others:
** Function List:
**   1.
** History:
**  <Author>    <date>    <content>    <version>
*******************************************************************************
*/
//*****************************************************************************
// File Include
//*****************************************************************************
#include <stdio.h>
#include "stdint.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "sl_btmesh_factory_reset.h"

//*****************************************************************************
// Local Defines
//*****************************************************************************
#define SCENE_MIN										  1
#define SCENE_CNT                     58        //限制最多58个场景
#define LIGHT_ON                      1
#define LIGHT_OFF                     0
#define LIGHT_MIN_GROUP_NUM						0xC000
#define LIGHT_MAX_GROUP_NUM           0xC03F		//筒灯和灯带组地址范围0xC000-0xC07F
#define CDN_LIGHTNESS_ZERO            0         //灭灯
#define CDN_LIGHTNESS_MIN             1         //最低亮度
#define CDN_LIGHTNESS_MAX             100       //最大亮度

//灯带/筒灯选择
#define CDN_LIGHT_BULB    1		  //1------------筒灯
#define CDN_LIGHT_STRIP   0			//1------------灯带

#define SCENE_POWER_OFF_MEMORY   1   //1------打开场景断电记忆，0------关闭场景断电记忆

#define POWER_SWITCH_RESET     1    //1----------开关复位功能，0--------取消开关复位功能

#define CDN_Lightness_ENABLE                1   //EN使能脚PD04，亮度大于0，为高；等于0，为低
#if CDN_Lightness_ENABLE
#define CDN_EN_PORT                    gpioPortD
#define CDN_EN_PIN                     4
#endif

#define GPIO_PIN_HIGH   1
#define GPIO_PIN_LOW    0

#define CDN_CCT_READ_PORT             gpioPortD
#define CDN_CCT_READ_PIN              0

#define AI_LIGHT_TYPE    0x81

#if CDN_LIGHT_STRIP
#define CDN_STRIP_DIMMING_LIGHT				1					//灯带单色灯、双色灯选择。1：单色灯 0：双色灯
#endif

#define CDN_DEBUG_TRACE               1
#if CDN_DEBUG_TRACE
#define TRACE(...)      printf(__VA_ARGS__)
#else
#define TRACE(...)
#endif

#define ERROR													0
#define SUCCESS 											1

#pragma pack(1)
//*****************************************************************************
// Local enums
//*****************************************************************************
typedef enum   //功率模式
{
  Power_default_Mode = 0,
  Power_5W_Mode = 1,
  Power_7W_Mode,
  Power_9W_Mode,
  Power_12W_Mode,
  Power_15W_Mode,
  Power_18W_Mode,
  Power_20W_Mode,
  Power_24W_Mode,
}CdnPowerMode;

//功率选择模式
typedef struct Power_SelectModeInfo
{
  uint8_t Power_Mode;
}Power_SelectModeInfo;

/*********************
 *
 ******************************/
typedef enum   //具体值根据实测电流设置---------只有一路DC-DC，4K频率，亮度一路，色温一路
{
  Power_5W = 640,    //电流100mA----------理论：621/1000---------(占空比数组：7142/20000)
  Power_7W = 760,    //电流150mA----------理论：754/1000---------(占空比数组：10714/20000)
  Power_9W = 870,    //电流200mA----------理论：860/1000---------(占空比数组：14285/20000)
  Power_12W = 1000,  //12W最大电流280mA---理论：1000/1000---------(占空比数组：20000/20000)
  Power_15W = 850,//电流350mA----------理论：853/1000---------(占空比数组：14000/20000)
  Power_18W = 930,//电流420mA----------理论：926/1000---------(占空比数组：16800/20000)
  Power_20W = 960,//电流450mA----------理论：955/1000---------(占空比数组：18000/20000)
  Power_24W = 1000,//电流500mA---------理论：1000/1000---------(占空比数组：20000/20000)
}CdnPowerValue;

//功率选择模式值
typedef struct Power_SelectInfo
{
  uint16_t PrePower_Value;
  uint16_t Power_Value;
}Power_SelectInfo;

/*********************
 *
 ******************************/
//灯设备类型
typedef enum{
  CDN_AI_NULL,
  CDN_AI_12W_BULB1  = 0x07,    //12W 单调光灯
  CDN_AI_12W_BULB2 = 0x08,   //12W 调光调色灯
  CDN_AI_24W_BULB1= 0x09,  //24W 单调光灯
  CDN_AI_24W_BULB2  = 0x0A,  //24W 调光调色灯
  CDN_AI_STRIP1  = 0x0B,//单调光灯带
  CDN_AI_STRIP2  = 0x0C,//调光调色灯带
}CdnLightDeviceID;


//灯状态
typedef enum
{
  CDN_LED_IDLE,
  CDN_LED_NO_NET,
  CDN_LED_CONFIG_BLINK,
  CDN_DIM_CT_LED_ON,       //调光调色，有渐变效果
  CDN_DIM_CT_LED_OFF,      //调光调色，有渐变效果
  CDN_LED_ON,
  CDN_LED_OFF,
  CDN_LED_FACTORY_BLINK,   //厂测模式快闪
  CDN_LED_NONE_STATE,
  CDN_LED_RESET_BLINK,//按键复位时闪烁
}CdnLedState;

//灯工作模式
typedef enum{
  CDN_WORK_MODE = 0,    //工作模式
  CDN_FACTORY_MODE,     //厂测模式
}CdnWorkMode;

//灯设备类型
typedef enum{
  CDN_DIMMING_LIGHT = 0, //调光灯
  CDN_CCT_LIGHT,         //色温灯
}CdnLightDevice;

//灯效模式
typedef enum
{
  EFFECT_NONE,
  EFFECT_BREATH,
  EFFECT_QUICK_BREATH,
}LIGHT_EFFECT;

//呼吸灯效
typedef enum{
  BREATH_UP = 0,
  BREATH_DOWN,
  BREATH_OVER,
}CDN_BREATH_CMD;

//*****************************************************************************
// Local structures
//*****************************************************************************
//灯当前状态
typedef struct LightCurrentState
{
  CdnLedState LightState;      //当前状态
  uint8_t Brightness;          //当前亮度
  uint8_t CTvalue;             //当前色温
  uint8_t TargetLightnes;      //目标亮度值
  uint8_t TargetCT;            //目标色温值
  uint8_t PreTargetLightness; //记录上一次亮度值
  uint16_t DimRemainTime;      //亮度渐变要求剩余时间
  uint16_t CTRemainTime;       //色温渐变要求剩余时间
  uint16_t DimIndex;           //亮度渐变取值，0-1000
  uint16_t CTIndex;            //色温渐变取值，0-1000
  uint8_t DimEndIndex;				 //亮度渐变取值个位，0-9
  uint8_t CTEndIndex;					 //色温渐变取值个位，0-9
  int8_t DimFadeFlag;          //1：渐亮 0：渐灭 -1：不变
  int8_t CTFadeFlag;           //1：渐亮 0：渐灭 -1：不变
}LightCurrentState;

//当前的组信息
typedef struct GroupInfo
{
  uint8_t Lightness;      						//当前回路亮度值，百分比0-100
  uint8_t CT;             						//当前回路色温值，百分比0-100
}GroupInfo;

//场景中的组信息
typedef struct GroupInSceneInfo
{
  uint8_t Lightness;      						//当前回路亮度值，百分比0-100
  uint8_t CT;             						//当前回路色温值，百分比0-100
	uint8_t SceneID;										//场景ID，判断该场景是否存在
}GroupInSceneInfo;

//场景信息
typedef struct SceneGroup
{
  GroupInSceneInfo GroupInScene;  //每个场景下包含的组信息
}SceneGroup;

//灯属性
typedef struct LightInfo
{
  uint16_t group_adr;                 //与灯绑定的组地址
  uint8_t GroupLeader;                //0:不是组长，1:组长
  GroupInfo CurrentGroup;             //当前组的亮度和色温信息
  uint8_t GroupLightOnOff;					  //组灯开关状态 0:关 1:开
  uint8_t LightRoomArea;					    //灯所在的房间
}LightInfo,*P_LightInfo;

//灯效模式
typedef struct 
{
	LIGHT_EFFECT mode;
	uint8_t time;
}LIGHT_EFFECT_CMD;


//*****************************************************************************
// Local function declaration
//*****************************************************************************
void SetGroupOnoffJudgeFlag(uint8_t Flag); //用于------点击开/关为2.5S渐变，滑动亮度条为5S渐变
uint8_t GetGroupOnoffJudgeFlag(void);

uint16_t cdn_get_element_address(void);
uint16_t cdn_get_product_id(void);
void cdn_get_node_information(void);
void cdn_get_node_primary_element_seqnum(void);
void LightPowerWorkModeInit(void);
void SetCurrentLightPowerWorkMode(uint8_t PowerMode);
void SetCurrentFactoryLightPowerWorkMode(uint8_t PowerMode);
uint8_t GetCurrentLightPowerWorkMode(void);
void SetCurrentGroupLightnessPowerValue(uint16_t GroupAdr,uint8_t PowerMode);
void SetCurrentLightPowerWorkModeValue(CdnPowerValue Value);
CdnPowerValue GetCurrentLightPowerWorkModeValue(void);
void SetCurrentLightPrePowerWorkModeValue(CdnPowerValue Value);
CdnPowerValue GetCurrentLightPrePowerWorkModeValue(void);
void SetLightCurrentValue(uint16_t Value);
uint16_t GetLightCurrentValue(void);



void SetCurrentLightWorkMode(CdnWorkMode Mode);
CdnWorkMode GetCurrentLightWorkMode(void);
void SetCurrentBrightness(uint8_t CurrentBrightness);
uint8_t GetCurrentBrightness(void);
void SetTargetBrightness(uint8_t TargetBrightness);
uint8_t GetTargetBrightness(void);
void SetPreTargetBrightness(uint8_t TargetBrightness);
uint8_t GetPreTargetBrightness(void);
void SetTargetCTvalue(uint8_t TargetCT);
uint8_t GetTargetCTvalue(void);
void SetCurrentCTvalue(uint8_t CT);
uint8_t GetCurrentCTvalue(void);
uint16_t ChangeToTargetBrightnessRemainTime(void);
void SetBrightnessRemainTime(uint16_t time);
uint16_t GetBrightnessRemainTime(void);
uint16_t ChangeToTargetCTRemainTime(void);
void SetCTRemainTime(uint16_t time);
uint16_t GetCTRemainTime(void);
#if CDN_LIGHT_BULB | CDN_LIGHT_STRIP
void SetCdnFactoryTestTimerStarted(uint8_t Flag);
uint8_t GetCdnFactoryTestTimerStarted(void);
void CdnFactoryTestStep(void);
#endif
void CdnStartFactoryTest(void);
void CdnStopFactoryTest(void);
void SetKeyDimmingUpDown(uint8_t cmd);
uint8_t GetKeyDimmingUpDown(void);
void SetKeyCTUpDowm(uint8_t cmd);
uint8_t GetKeyCTUpDowm(void);
CdnLedState GetLightState(void);
void Light_GPIO_Init(void);
CdnLightDevice GetCdnLightDeviceType(void);
void CDN_Adv_Type(uint8_t *type);
void SetLedState(CdnLedState LedState,uint8_t brightness,uint8_t CTvalue);
void CdnLedStateHandle(void);
void SetLightnessCTFadesCtrl(uint8_t Lightness,uint8_t CT);
void LightnessDimmingCtrlByKey(uint16_t GroupAdr, uint8_t Cmd);
void LightCTChangeByKeyClick(uint16_t GroupAddr, uint8_t CCT_Cmd,uint8_t AdjRoomArea);
void LightBroadcastCurrentDim(void);
void LightBroadcastCurrentCt(void);

void LightnessCCTCtrlByKey(uint16_t GroupAddr,uint8_t CCT_Cmd,uint8_t AdjRoomArea);
void SetProvisionFlag(uint8_t provisioned);
uint8_t GetProvisionFlag(void);
void CheckGroupLeaderAck(uint16_t GroupAdr,uint16_t adr_src);
void LedGroupBind(uint16_t GroupAdr,uint8_t GroupLeader,uint8_t RoomAreaBind);
void LedInfoAck(uint16_t adr_src);

void SetCurrentGroupLightnessCT(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT);

void VoiceSetGroupLightnessCtAdj(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT);
void VoiceSetRoomLightnessCtAdj(uint8_t RoomId,uint8_t Lightness,uint8_t CT);
void VoiceCtrlAllBulbOnOff(uint8_t OnOff);

void LedCurrentGroupOnJudge(uint16_t GroupAdr);
void LedCurrentGroupOffJudge(uint16_t GroupAdr);

void LedCurrentGroupOnOffCtrl(uint16_t GroupAdr,uint8_t OnOff);
void LedCurrentRoomOnOffCtrl(uint8_t RoomId,uint8_t OnOff);

void DeleteCurrentGroupSceneId(uint8_t Sceneid,uint16_t Groupall);
void SetSceneGroupLightnessCT(uint8_t Sceneid,uint16_t GroupAdr,uint8_t Lightness, uint8_t CT);
void ConfirmSceneGroupLightnessCT(uint8_t Sceneid,uint16_t GroupAdr,uint8_t Lightness, uint8_t CT);

#if SCENE_POWER_OFF_MEMORY
void SetLightSceneMemoryID(uint8_t Sceneid);
void SetPreLightSceneMemoryID(uint8_t Sceneid);
void SetFirstPowerOnFlag(uint8_t Flag);
void CheckLightSceneInfoIfChange(void);
void LightSceneMemoryInit(void);
void ExecuteMemorySceneId(void);
#endif

void SceneRecall(uint8_t Sceneid,uint8_t OnOff);
void DeleteCurrentSceneid(uint8_t Sceneid);
void DeleteGroup(uint16_t GroupAdr,uint8_t Reset);
void EnteringGroup(void);
uint8_t GetEnterGroupInfo(void);
void EnterGroupSuccess(void);

void SetConfigBlinkFlag(uint8_t Flag);
uint8_t GetConfigBlinkFlag(void);
void FailedToEnterNetworkGroup(void);
void SetSaveFlashFlag(uint8_t FlashFlag);
void CheckLightInfoIfChange(void);
void ReadLightInfoFromFlash(void);
void GroupLightnessCCTSync(uint16_t GroupAdr,uint8_t Lightness,uint8_t CT);

void CDN_Light_effect_display(void);
void Set_CDN_Light_effect_mode(LIGHT_EFFECT mode, uint8_t time);
LIGHT_EFFECT Get_CDN_Light_effect_mode(void);
uint8_t Get_CDN_Light_effect_time(void);
uint8_t is_valid_group(uint16_t GroupAdr);
uint8_t is_valid_scene(uint8_t Sceneid);
uint8_t is_valid_device(uint8_t Pid, uint8_t Sid);
#endif

